package com.andyliu.dict.controller;


import com.andyliu.Constants;
import com.andyliu.common.ResponseResult;
import com.andyliu.controller.BaseController;
import com.andyliu.dict.entity.SysDictData;
import com.andyliu.dict.service.ISysDictDataService;
import com.andyliu.interceptor.BusinessType;
import com.andyliu.log.Log;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author andyLiu
 * @since 2021-10-08
 */
@Slf4j
@Controller
@RequestMapping("/dict/sysDictData")
@Api(value = "字典数据表控制器" , tags = "字典数据表操作接口")
public class SysDictDataController extends BaseController {
    @Autowired
    private ISysDictDataService service;
    /**
    * 跳转到列表页
    */
    @GetMapping()
    @ApiIgnore
    public String toData(){
            log.debug("跳转到列表页");
            return"dict/SysDictData/data" ;
            }
    /**
    * 异步加载分页数据
    */
    @PostMapping("data")
    @ResponseBody
    @ApiOperation(value = "字典数据表分页数据接口" , notes = "根据查询和分页条件返回分页数据" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber" , value = "分页页码" , required = true, type = "Integer"),
            @ApiImplicitParam(name = "pageSize" , value = "每页显示个数" , required = true, type = "Integer"),
            @ApiImplicitParam(name = "obj" , value = "用于分装实体的查询条件" , required = false)
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "字典数据表数据分页",businessType = BusinessType.QUERY)
    public ResponseResult data(SysDictData obj,@RequestParam(required = true, defaultValue = "1") Integer pageNumber,@RequestParam(required = true, defaultValue = Constants.DEFAULT_PAGE_SIZE) Integer pageSize){
            log.debug("分页查询SysDictData数据");
            IPage<SysDictData> page=service.selectByPage(pageNumber,pageSize,obj);
            return ResponseResult.success(page);
            }
    /**
    * 跳转到新增编辑页面
    */
    @GetMapping("/edit/{id}")
    @ApiIgnore
    @Log(moduleName = "字典数据表数据详情",businessType = BusinessType.QUERY)
    public String toEidt(@PathVariable Integer id,ModelMap mmap){
            if(id!=-1){
        SysDictData sysDictData=service.getById(id);
            mmap.put("sysDictData" ,sysDictData);
            }
            return"dict/SysDictData/edit" ;
            }
    @GetMapping("/info/{id}")
    @ResponseBody
    @ApiOperation(value = "根据主键获取字典数据表数据接口" , notes = "根据主键获取实体数据" , httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "主键" , required = true, type = "Integer")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "字典数据表数据详情",businessType = BusinessType.QUERY)
    public ResponseResult toEidt(@PathVariable Integer id){
            log.debug("查看字典数据表的数据,id为" +id);
        SysDictData obj=service.getById(id);
            return ResponseResult.success(obj);
            }

    /**
    * 异步保存或新增
    */
    @PostMapping("save")
    @ResponseBody
    @ApiOperation(value = "保存或更新字典数据表数据接口" , notes = "如果实体类中有主键为更新，否则为保存" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user" , value = "用户信息表实体" , required = true, type = "Integer")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "字典数据表数据保存",businessType = BusinessType.INSERT)
    public ResponseResult save(@RequestBody SysDictData sysDictData){
            log.debug("保存字典数据表的数据");
                    if(sysDictData.getDictCode()!=null){
                    boolean status=service.updateById(sysDictData);
                    if(status){
                    return ResponseResult.success();
                    }else{
                    return ResponseResult.error();
                    }
                    }
            return service.save(sysDictData)==true?
            ResponseResult.success()
            :ResponseResult.error();
            }
    /**
    * 删除或批量删除请求
    */
    @DeleteMapping("delete")
    @ResponseBody
    @ApiOperation(value = "批量删除字典数据表数据接口" , notes = "批量删除" , httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids" , value = "需要删除的主键的数组" , required = true, type = "Integer")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "字典数据表数据删除",businessType = BusinessType.DELETE)
    public ResponseResult delete(Integer[]ids){
            log.debug("删除字典数据表的数据,id为" +ids);
            boolean status = service.removeByIds(Arrays.asList(ids));
            if(status){
                return ResponseResult.success("删除成功！");
            }
            return ResponseResult.error();
    }
    /**
    * 单个删除
    */
    @DeleteMapping("delete/{id}")
    @ResponseBody
    @ApiOperation(value = "删除字典数据表数据接口" , notes = "单个删除，根据主键删除" , httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "需要删除主键" , required = true, type = "Integer")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "字典数据表数据删除",businessType = BusinessType.DELETE)
    public ResponseResult delete(@PathVariable Integer id){
            log.debug("删除字典数据表的数据,id为" +id);
            boolean status=service.removeById(id);
            if(status){
            return ResponseResult.success("删除成功！");
            }
            return ResponseResult.error();
            }
    }



